package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysDiagnosisTask;
import com.ruoyi.system.service.ISysDiagnosisTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 诊断任务列Controller
 * 
 * @author ruoyi
 * @date 2022-05-22
 */
@RestController
@RequestMapping("/system/diagnosistask")
public class SysDiagnosisTaskController extends BaseController
{
    @Autowired
    private ISysDiagnosisTaskService sysDiagnosisTaskService;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询诊断任务列列表
     */
  //  @PreAuthorize("@ss.hasPermi('system:task:list')")
    @GetMapping("/list")
    public AjaxResult list(SysDiagnosisTask sysDiagnosisTask)
    {
        startPage();
        List<SysDiagnosisTask> list = sysDiagnosisTaskService.selectSysDiagnosisTaskList(sysDiagnosisTask);

        HashMap<Long,String> companyNameMap=deptService.getCompanyNameMap();
        AjaxResult result=AjaxResult.success();


        result.put("companyNameMap",companyNameMap);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        result.put("tableInfo",rspData);
        return result;
    }

    /**
     * 导出诊断任务列列表
     */
   // @PreAuthorize("@ss.hasPermi('system:task:export')")
    @Log(title = "诊断任务列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDiagnosisTask sysDiagnosisTask)
    {
        List<SysDiagnosisTask> list = sysDiagnosisTaskService.selectSysDiagnosisTaskList(sysDiagnosisTask);
        ExcelUtil<SysDiagnosisTask> util = new ExcelUtil<SysDiagnosisTask>(SysDiagnosisTask.class);
        util.exportExcel(response, list, "诊断任务列数据");
    }

    /**
     * 获取诊断任务列详细信息
     */
   // @PreAuthorize("@ss.hasPermi('system:task:query')")
    @GetMapping(value = "/selectbyids")
    public AjaxResult getInfo(SysDiagnosisTask sysDiagnosisTask)
    {
        return AjaxResult.success(sysDiagnosisTaskService.selectSysDiagnosisTaskList(sysDiagnosisTask).get(0));
    }

    /**
     * 获取诊断任务列详细信息
     */
   // @PreAuthorize("@ss.hasPermi('system:task:query')")
    @GetMapping(value = "/getundocompanys")
    public AjaxResult getUndoCompanys()
    {
        SysDept dept=new SysDept();
        dept.setParentId(0L);
        List<SysDept> companys=deptService.selectDeptList(dept);
        List<SysDiagnosisTask> tasks=sysDiagnosisTaskService.selectSysDiagnosisTaskList(new SysDiagnosisTask());
        List<Long> dones=new ArrayList<>();
        for(SysDiagnosisTask task :tasks){
            dones.add(task.getCompanyId());
        }
        List<SysDept> undos=new ArrayList<>();

        for(SysDept company :companys){
            if(dones.contains(company.getDeptId()))
                continue;
            undos.add(undos.size(),company);

        }
        return AjaxResult.success(undos);
    }

    /**
     * 新增诊断任务列
     */
 //   @PreAuthorize("@ss.hasPermi('system:task:add')")
    @Log(title = "诊断任务列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDiagnosisTask sysDiagnosisTask)
    {
        return toAjax(sysDiagnosisTaskService.insertSysDiagnosisTask(sysDiagnosisTask));
    }

    /**
     * 修改诊断任务列
     */
   // @PreAuthorize("@ss.hasPermi('system:task:edit')")
    @Log(title = "诊断任务列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDiagnosisTask sysDiagnosisTask)
    {
        return toAjax(sysDiagnosisTaskService.updateSysDiagnosisTask(sysDiagnosisTask));
    }

    /**
     * 删除诊断任务列
     */
 //   @PreAuthorize("@ss.hasPermi('system:task:remove')")
    @Log(title = "诊断任务列", businessType = BusinessType.DELETE)
	@DeleteMapping("/{companyIds}")
    public AjaxResult remove(@PathVariable Long[] companyIds)
    {
        return toAjax(sysDiagnosisTaskService.deleteSysDiagnosisTaskByCompanyIds(companyIds));
    }
}
